<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\adminapi\controller\auth;

use app\admin\model\AuthRule;
use app\common\controller\BackendApi;
use fast\Tree;
use think\Cache;
use think\Db;
use think\exception\PDOException;

/**
 * 规则管理
 *
 * @icon   fa fa-list
 * @remark 规则通常对应一个控制器的方法,同时左侧的菜单栏数据也从规则中体现,通常建议通过控制台进行生成规则节点
 */
class Rule extends BackendApi
{

    /**
     * @var \app\admin\model\AuthRule
     */
    protected $model = null;
    protected $rulelist = [];
    protected $multiFields = 'ismenu,status';

    public function _initialize()
    {
        parent::_initialize();
        if (!$this->auth->isSuperAdmin()) {
            $this->error(__('Access is allowed only to the super management group'));
        }
        $this->model = new AuthRule();
    }
    /**
     * 查看
     */
    public function index()
    {
        $data = $this->auth->getMenu();
        $this->success('获取成功', $data);
    }

    /**
     * 添加
     */
    public function add()
    {
        $pid = $this->request->post('pid') ?? 0;
        $url = $this->request->post('url') ?? '';
        $name = $this->request->post('name');
        $title = $this->request->post('title');
        $front_icon = $this->request->post('front_icon');
        $menutype = $this->request->post('menutype');
        $ismenu = $this->request->post('ismenu');
        $extend = $this->request->post('extend');
        $router = $this->request->post('router');
        $weigh = $this->request->post('weigh');
        if (!$ismenu && !$pid) {
            $this->error(__('The non-menu rule must have parent'));
        }
        $data['pid'] = $pid;
        $url ? $data['url'] = $url : '';
        $name ? $data['name'] = $name : $this->error(__('Name').'不能为空');
        $title ? $data['title'] = $title : $this->error(__('Title').'不能为空');
        $front_icon ? $data['front_icon'] = $front_icon : $this->error(__('Icon').'不能为空');
        $menutype ? $data['menutype'] = $menutype : $this->error(__('Menutype').'不能为空');
        $ismenu ? $data['ismenu'] = $ismenu : '';
        $extend ? $data['extend'] = $extend : '';
        $weigh ? $data['weigh'] = $weigh : '';
        $router ? $data['router'] = $router : '';
        $result = $this->model->validate()->save($data);
        if ($result === false) {
            $this->error($this->model->getError());
        }
        Cache::rm('__menu__');
        $this->success('添加成功');
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $ids = $this->request->post('ids');
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $pid = $this->request->post('pid') ?? 0;
        $url = $this->request->post('url') ?? '';
        $name = $this->request->post('name');
        $title = $this->request->post('title');
        $front_icon = $this->request->post('front_icon');
        $router = $this->request->post('router');
        $menutype = $this->request->post('menutype');
        $ismenu = $this->request->post('ismenu');
        $extend = $this->request->post('extend') ?? '';
        $weigh = $this->request->post('weigh') ?? 0;
        if (!$ismenu && !$pid) {
            $this->error(__('The non-menu rule must have parent'));
        }

        if ($pid == $row['id']) {
            $this->error(__('Can not change the parent to self'));
        }

        if ($pid != $row['pid']) {
            $childrenIds = Tree::instance()->init(collection(AuthRule::select())->toArray())->getChildrenIds($row['id']);
            if (in_array($pid, $childrenIds)) {
                $this->error(__('Can not change the parent to child'));
            }
        }
        $data['pid'] = $pid;
        $url ? $data['url'] = $url : '';
        $name ? $data['name'] = $name : $this->error(__('Name').'不能为空');
        $title ? $data['title'] = $title : $this->error(__('Title').'不能为空');
        $front_icon ? $data['front_icon'] = $front_icon : $this->error(__('Icon').'不能为空');
        $menutype ? $data['menutype'] = $menutype : $this->error(__('Menutype').'不能为空');
        $ismenu ? $data['ismenu'] = $ismenu : '';
        $extend ? $data['extend'] = $extend : '';
        $weigh ? $data['weigh'] = $weigh : '';
        $router ? $data['router'] = $router : '';
        $ruleValidate = \think\Loader::validate('AuthRule','validate',false,'adminapi');
        $ruleValidate->rule([
            'name' => 'require|unique:AuthRule,name,' . $row->id,
        ]);
        $result = $row->validate('AuthRule.edit')->save($data);
        if ($result === false) {
            $this->error($row->getError());
        }
        Cache::rm('__menu__');
        $this->success('操作成功');
    }


}
